using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;

namespace Utils
{
    public class NameValues
    {
        public NameValues()
        {

        }

        public void AddNameValues(NameValues src)
        {
            foreach(string name in src.nameValues.Keys)
            {
                nameValues[name] = src[name];
            }
        }

        public bool IsNamesEmpty
        {
            get { return nameValues.Count == 0; }
        }

        public object this[string name]
        {
            get { return nameValues[name];  }
            set { nameValues[name] = value; }
        }

        public bool ExistsName(string name)
        {
            return nameValues.ContainsKey(name);
        }

        public T Value<T>(string name, T defValue)
        {
            if (nameValues.Contains(name))
            {
                return (T)nameValues[name];
            }

            return defValue;
        }

        public T Value<T>(string name)
        {
            return (T)nameValues[name];
        }

        public int IntValue(string name, int defValue)
        {
            try
            {
                if (nameValues.ContainsKey(name))
                {
                    return Convert.ToInt32(nameValues[name]);
                }

            }
            catch
            {

            }

            return defValue;
        }

        public int IntValue(string name)
        {
            try
            {
                if (nameValues.ContainsKey(name))
                {
                    return Convert.ToInt32(nameValues[name]);
                }
            }
            catch
            {

            }

            return 0;
        }

        public static string operator ^(string name, NameValues nameValues)
        {
            object value = nameValues[name];
            return value == null ? string.Empty : value.ToString().Trim();
        }

        public string[] Names
        {
            get
            {
                Array names = Array.CreateInstance(typeof(string), nameValues.Count);
                nameValues.Keys.CopyTo(names, 0);
                return (string[])names;
            }
        }

        public void Clear() 
        {
            nameValues.Clear();
        }

        public void Remove(string name)
        {
            nameValues.Remove(name);
        }

        protected Hashtable nameValues = new Hashtable();
    }
}
